<?php
	####################################################
	# Ncurl URL CHECKER                                #
	####################################################

	class Ncurl {
		# URL 존재확인
		public static function exists_url($url) {
			$url = str_replace("http://", "", $url);
			list($domain, $file) = explode("/", $url, 2); # 도메인부분과 주소부분으로 나눕니다.
			$fid = fsockopen($domain, 80); # 도메인을 오픈합니다.
			fputs($fid, "GET /$file HTTP/1.0\r\nHost: $domain\r\n\r\n"); # 파일 정보를 얻습니다.
			$gets = fgets($fid, 128);
			fclose($fid);
			if (ereg("200 OK", $gets)) return TRUE;
			else return FALSE;
		}

		# 도메인 또는 문서가 존재하는지 검사
		public static function exists_url_domain($url, $port = "80") {
			$fp = @fsockopen($url, $port);
			if ($fp) return true;
			else return false;
		}

		# 외부 파일 읽기
		public static function readurl($str,$port=80) {
			$url = parse_url($str);

			$fp = @fsockopen($url[host], $port,$errno,$errstr,10);
			if ($fp){
				fwrite($fp, "GET $url[path]?$url[query] HTTP/1.0\r\nHost: $url[host]\r\n\r\n");
				while (!feof($fp)) $out .= fread($fp, 1024);
				fclose($fp);

				$out = explode("\r\n\r\n",$out);
				array_shift($out);
				$out = implode("",$out);
			}

			return $out;
		}

		# 외부 파일 읽기(POST)
		public static function readpost($str, $data, $port=80) {
			$url = parse_url($str); # parsing the given URL
			$referrer = $_SERVER['SCRIPT_URL']; #  Building referrer
			if (!isset($url[port])) $url[port] = $port; # find out which port is needed(=80)
			$data_string = getVars('', $data); # making string from $data

			### building POST-request:
			$request .= "POST {$url[path]} HTTP/1.1\r\nHost: {$url[host]}\r\nReferer: {$referrer}\r\n";
			$request .= "Content-type: application/x-www-form-urlencoded\r\n";
			$request .= "Content-length: " . strlen($data_string) . "\r\n";
			$request .= "Connection: close\r\n\r\n";
			$request .= "{$data_string}\r\n";

			### connect socket
			$fp = @fsockopen($url[host], $url[port], $errno, $errstr, 10);
			if ($fp){
				fwrite($fp, $request);
				do $header .= fread($fp, 1); while (!preg_match('/\\r\\n\\r\\n$/', $header)); # get header

				### get content
				if (preg_match('/Transfer\\-Encoding:\\s+chunked\\r\\n/', $header, $matches)) # check for chunked encoding
					do {
						$byte = $chunk_size = "";
						do { $chunk_size .= $byte; $byte = fread($fp, 1); } while ($byte != "\r"); # till we match the CR
						fread($fp, 1); # also drop off the LF
						$chunk_size = hexdec($chunk_size); # convert to real number
						if ($chunk_size) $out .= fread($fp, $chunk_size);
						fread($fp, 2); # ditch the CRLF that trails the chunk
					} while ($chunk_size); # till we reach the 0 length chunk (end marker)
				else if (preg_match('/Content\\-Length:\\s+([0-9]*)\\r\\n/', $header, $matches)) $out = fread($fp,$matches[1]); # check for specified content length
				else while (!feof($fp)) $out .= fread($fp, 4096); # not a nice way to do it

				fclose($fp); # close connection
			}

			return $out;
		}
	}

?>